package com.caipiao.service.impl;

import com.caipiao.mapper.*;
import com.caipiao.pojo.*;
import com.caipiao.service.IndexService;
import com.caipiao.util.CommonUtils;
import com.caipiao.util.ConfigUtils;
import com.caipiao.util.Page;
import com.caipiao.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2019/1/7.
 */
@Service
@Transactional
public class IndexServiceImpl implements IndexService {

    private static final Logger LOG = LoggerFactory.getLogger(IndexServiceImpl.class);

    @Autowired
    private SMemberMapper sMemberMapper;
    @Autowired
    private SAdvertisementMapper sAdvertisementMapper;
    @Autowired
    private SPlanMapper sPlanMapper;
    @Autowired
    private SUserMemberMapper sUserMemberMapper;

    @Override
    public ObjectResult index(XMLBean xmlBean) {
        //轮播图
        List<SAdvertisement> sAdvertisements = sAdvertisementMapper.selectAdvertisementList();

        //专家列表
        List<SMember> sMembers = sMemberMapper.selectMember(null);
        Page<SMember> page = new Page<>();
        page.setP(1);
        page.setPageSize(7d);
        List<SMember> pageContent = page.getPageContent(sMembers);

        for (SMember sMember:pageContent){
            //擅长联赛
            sMember.setList(sMemberMapper.selectMemberLeague(sMember.getId()));
            sMember.setHead(ConfigUtils.ADMIN_URL+sMember.getHead());

            //是否关注
            if(!StringUtil.isEmpty(xmlBean.getToken())){
                String userId = CommonUtils.decToken(xmlBean.getToken());
                SUserMember member = new SUserMember();
                member.setUserId(Integer.parseInt(userId));
                member.setMemberId(sMember.getId());
                Integer isFollow = sUserMemberMapper.select(member);
                if (isFollow == null){
                    sMember.setIsFollow(0);
                }else {
                    sMember.setIsFollow(isFollow);
                }
            }else {
                sMember.setIsFollow(0);
            }
        }

        //专家策略跟单
        List<SMember> list = sMemberMapper.selectMemberOne();
        page.setP(xmlBean.getP()==null ? 1:xmlBean.getP());
        page.setPageSize(10);
        List<SMember> pageContent1 = page.getPageContent(list);
        for (SMember sMember:pageContent1) {
            sMember.setHead(ConfigUtils.ADMIN_URL+sMember.getHead());
            Map<String, Object> map = new HashMap<>();
            map.put("memberId", sMember.getMemberId());
            map.put("matchState","(3)");
            List<SPlan> sPlans = sPlanMapper.selectplantByMemberId(map);

            int total = 0;
            int hit = 0;
            int count = 0;
            if (sPlans.size() > 0) {
                //近期命中
                if (sPlans.size() >= 10) {
                    total = 10;
                    for (int i = 0; i < 10; i++) {
                        if (sPlans.get(i).getIsWin() == 1) {
                            hit++;
                        }
                    }
                } else {
                    total = sPlans.size();
                    for (int i = 0; i < sPlans.size(); i++) {
                        if (sPlans.get(i).getIsWin() == 1) {
                            hit++;
                        }
                    }
                }
                sMember.setHit("近"+total + "场中" + hit+"场");

                //总命中率
//                for (int i = 0; i < sPlans.size(); i++) {
//                    if (sPlans.get(i).getIsWin() == 1) {
//                        count++;
//                    }
//                }
//
//
//                    BigDecimal divide = new BigDecimal(count*100).divide(new BigDecimal(sPlans.size()),0, RoundingMode.HALF_UP);
//
//                    sMember.setHitRate(divide);

            }else {
                sMember.setHit("");
            }
        }
        //首页最新方案弹窗
        List<SPlan> sPlan = sPlanMapper.selectNewPlan();
        //最近中奖方案
        List<String> win = sPlanMapper.selectIsWin();
        Map<String,Object> map = new HashMap<>();
        map.put("carousel",sAdvertisements);
        map.put("memberList",pageContent);
        map.put("list",pageContent1);
        map.put("count",page.getPageTotal());
        map.put("newPlan",sPlan);
        map.put("win",win);
        if (sPlan.size()<1){
            map.put("isShow",0);
        }else {
            map.put("isShow",1);
        }

        if (win.size()<1){
            map.put("isShow2",0);
        }else {
            map.put("isShow2",1);
        }

        return ObjectResult.build("1","成功",map);
    }


}
